package com.luo.bbs.front.webapi.controller;


import com.example.bbs.front.service.IQuestionService;
import com.luo.common.restful.JsonPage;
import com.luo.common.restful.JsonResult;

import com.luo.pojo.question.dto.QuestionDTO;
import com.luo.pojo.question.vo.QuestionVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Path;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bin.cn
 * @since 2023-01-19
 */
@RestController
@Slf4j
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private IQuestionService questionService;

    @PostMapping("/post")
    public JsonResult post(@Validated QuestionDTO questionDTO) {
        QuestionVO questionVO = questionService.post(questionDTO);
        log.debug("帖子发表成功！");
        return JsonResult.ok(questionVO);
    }

    /**
     * 查询某个版块下的帖子列表
     *
     * @param title
     * @return
     */
    @GetMapping("/get/{title}")
    public JsonResult getByTag(@PathVariable String title, Integer userId) {
        List<QuestionVO> list = questionService.getByTag(title, userId);
        return JsonResult.ok(list);
    }

    /**
     * 通过ID查询帖子详情
     */
    @GetMapping("/get/detail/{id}")
    public JsonResult getById(@PathVariable Integer id, Integer userId) {
        QuestionVO questionVO = questionService.getById(id, userId);
        return JsonResult.ok(questionVO);
    }

    /**
     * 查询用户发帖数
     */
    @GetMapping("/count/{userId}")
    public JsonResult countByUser(@PathVariable Integer userId) {
        Integer integer = questionService.countByUser(userId);
        return JsonResult.ok(integer);
    }


    /**
     * 用户收藏帖子
     */
    @PostMapping("/collect/{questionId}")
    public JsonResult collect(@PathVariable Integer questionId) {
        int collect = questionService.collect(questionId);
        return JsonResult.ok(collect);
    }

    /**
     * 分页查询所有帖子
     */
    @GetMapping("/get/all")
    public JsonResult getALL(Integer pageNum, Integer pageSize) {
        JsonPage<QuestionVO> questionPage = questionService.getAll(pageNum, pageSize);
        return JsonResult.ok(questionPage);
    }

    /**
     * 查询某个用户发帖
     */
    @GetMapping("/my/{userId}")
    public JsonResult getMy(@PathVariable Integer userId) {
        List<QuestionVO> questionVOList = questionService.getMy(userId);
        return JsonResult.ok(questionVOList);
    }

}
